<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Pago_mensual extends CI_Controller {

    public function index($success_message2 = null) {
        $this->load->model('Client_type_model');
        $this->load->model('Clients_model');
        $vars['typeClient'] = $this->Client_type_model->getAll();
        $vars['clients'] = $this->Clients_model->getAll();
        $vars['searchsales'] = true;
        if (isset($success_message2)) {
            $vars['success_message2'] = true;
        }
        $this->load->model('Client_type_model');        
        $vars['tclients'] = $this->Client_type_model->getAll();
        $vars['contacto'] = $this->Client_type_model->getAll2();
        $vars['parametro'] = true;
        $vars['content_view'] = 'contrato_form';
        $vars['menu'] = 'sales';
        $this->load->view('template', $vars);
    }

    public function search($edit = null) {
        
        $this->load->model('Contrato_model');
        $this->load->model('Client_type_model');
        $this->load->model('Clients_model');
        $vars['typeClient'] = $this->Client_type_model->getAll();
        $vars['clients'] = $this->Clients_model->getAll();
        
        $document_code = $this->input->post('numero');
        $datesale = $this->Contrato_model->search_contrato($document_code);
        
        $this->load->model('Client_type_model');        
        $vars['tclients'] = $this->Client_type_model->getAll();
        $vars['contacto'] = $this->Client_type_model->getAll2();
        
        $vars['fecha'] =  date('d/m/Y');        
        $vars['datesale'] = $datesale;
        $vars['contandodatos'] = count($datesale);        
        $vars['searchsales'] = true;
        $vars['content_view'] = 'contrato_form';
        $vars['menu'] = 'sales';
        $this->load->view('template', $vars);
        
    }

    public function codeguia() {
        $this->load->model('Client_type_model');
        $codeguia = $this->Client_type_model->generanum2();
        
        foreach ($codeguia as $c) {
            $codestring = $c->document_code;
            $num = (int) $codestring + 1;
            echo str_pad((int) $num, 12, "0", STR_PAD_LEFT);
        }
        
    }

    public function add($success_message = null) {
        $this->load->model('Client_type_model');
        $this->load->model('Clients_model');
        $vars['typeClient'] = $this->Client_type_model->getAll();
        $vars['clients'] = $this->Clients_model->getAll();
        if (isset($success_message)) {
            $vars['success_message'] = true;
        }
        $this->load->model('Client_type_model');        
        $vars['tclients'] = $this->Client_type_model->getAll();
        $vars['contacto'] = $this->Client_type_model->getAll2();
        
        $vars['content_view'] = 'pago_mensual_form';        
        $vars['menu'] = 'products';
        $this->load->view('template', $vars);
    }

    

    public function client() {
        $idClient = $this->input->post('id');
        $this->load->model('Clients_model');
        $dataClient = $this->Clients_model->find($idClient);
        echo json_encode($dataClient);
    }

    public function clients() {
        $document = null;
        $name = null;
        $this->load->model('Clients_model');
        if ($this->input->post('document_client')) {
            $document = $this->input->post('document_client');
        }
        if ($this->input->post('name')) {
            $name = $this->input->post('name');
        }
        $clients = array();
        if ($document != null || $name != null) {
            $clients = $this->Clients_model->search2($document, $name);
        }
        $vars['clients'] = $clients;
        $vars['document'] = $document;
        $vars['name'] = $name;
        $this->load->view('clients_list', $vars);
    }

    public function contratosbyclient() {
        
        $this->load->model('Contrato_model');
                
        $id_client = $this->input->post('id_cliente');
        $document_client = $this->input->post('document_client');
        $name = $this->input->post('name');
        
        $contratos = $this->Contrato_model->search_contrato2($id_client);
              
        $vars['id_client'] = $id_client;
        $vars['document_client'] = $document_client;
        $vars['name'] = $name;      
        $vars['contratos'] = $contratos;           
        $this->load->view('list_contratos', $vars);
    }
    
     public function fechas() {
        
        $this->load->model('Contrato_model');
                        
        $document_contrato = $this->input->post('document_contrato');
        
        $datos = $this->Contrato_model->ufechapago($document_contrato); 
        
        $vars['datos'] = $datos;           
        
        $this->load->view('fechasbycontratos', $vars);
    }

    public function submit() {
        
       if($this->input->post('id_contrato')){

                $this->load->model('Contrato_model');
                $id_contrato = $this->input->post('id_contrato');
                $numero = $this->input->post('numero');
                $duracion = $this->input->post('duracion');
                $monto = $this->input->post('monto');
                $client = $this->input->post('client');
                if ($client == 0) {
                    $client = "";
                }
                $observations = $this->input->post('observations');
                if ($observations == '') {
                    $observations = '';
                }    
                                
                $dateie = $this->input->post('date');
                $datee = str_replace('/', '-', $dateie);
                $datefe = date("Y-m-d", strtotime($datee) );
                
                $dateii = $this->input->post('datei');
                $datei = str_replace('/', '-', $dateii);
                $datefi = date("Y-m-d", strtotime($datei) );  
                
                $dateic = $this->input->post('datec');
                $datec = str_replace('/', '-', $dateic);
                $datefc = date("Y-m-d", strtotime($datec) );  
                
                            
                $data = array(
                    'document_code' => $numero,                    
                    'id_cliente' => $client,
                    'fecha_emision' => $datefe,
                    'fecha_inicio' => $datefi,
                    'fecha_cancelacion' => $datefc,
                    'monto' => $monto,
                    'duracion' => $duracion,
                    'estado' => 1,
                    'observations' => $observations
                    );

                $this->Contrato_model->update_contrato($id_contrato, $data);
          
                $this->index(true);

       }else{
            
            
                $this->load->model('Pago_mensual_model');
                $this->load->model('Contrato_model');
                $numero = $this->input->post('numero');
                $id_tpago = $this->input->post('tpago');
                $id_tdocument = $this->input->post('tdocument');
                
                $document_contrato = $this->input->post('document_contrato');
                $document_pago = $this->input->post('numero');
                $fecha_emision = $this->input->post('date');
                $monto = $this->input->post('monto');
                $saldo = $this->input->post('saldo');
                $nuevosaldo = $saldo - $monto;
                $observaciones = $this->input->post('observations');
                                                                                                
                $datee = str_replace('/', '-', $fecha_emision);
                $fechae = date("Y-m-d", strtotime($datee) );
                                
                            
                $data = array(
                    'document_code' => $numero,                    
                    'id_medio_pago' => $id_tpago,
                    'id_type_document' => $id_tdocument,
                    'referencia' => "xD"
                    );
                
                $data2 = array(
                    'id_contrato' => $document_contrato,                    
                    'id_pago' => $document_pago,
                    'fecha_emision' => $fechae,
                    'monto_pago' => $monto,
                    'observations' => $observaciones
                    );
                 
                 $this->Contrato_model->edit_saldo($document_contrato,$nuevosaldo);
                 $this->Pago_mensual_model->add_pago_mensual($data);
                 $this->Pago_mensual_model->add_detalle_pago_mensual($data2);

          $this->add(true); /*si es cierto grabara y llamara a la funcion add el cual necesita un parametro, le enviamos true.. que significa 1 y ya la funcion add ara lo suyo*/   

       }
    }
    
    public function imprimir()
    {   
                $this->load->model('Clients_model');
                $this->load->model('Contrato_model');                                 
                $duracion = $this->input->post('duracion');
                $monto = $this->input->post('monto');
                $client = $this->input->post('client');
                
                $f_client = $this->Clients_model->find($client);
                
                $fcliente = array();
                
                $fcliente[0] = $f_client->name;
                $fcliente[1] = $f_client->direccion;
                $fcliente[2] = $f_client->document_client;
                $fcliente[3] = $f_client->telefono;
                
                
                include('ControlContrato.php');
		$control=new ControlContrato;
		
		$pdf = $control->VerContrato($fcliente,$monto,$duracion);                                                                                                                                 
                

        
    }

    
    public function delete_detail() {

        $this->load->model('Sales_details_model');
        $document_code = $this->input->post('numero');
        $code = $this->input->post('code');

        echo $this->Sales_details_model->delete_detail($document_code, $code);
    }

    public function edit_amout() {
        $this->load->model('Sales_model');
        $document_code = $this->input->post('numero');
        $amount = $this->input->post('amount');

        $this->Sales_model->edit_amout($document_code, $amount);
    }

    public function search_code_unique() {
        $this->load->model('Contrato_model');
        $document_code = $this->input->post('numero');

        echo $this->Contrato_model->comparaction_code($document_code);
    }

}
?>